package com.example.service

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.IService
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.example.entity.User
import com.example.mapper.UserMapper
import org.springframework.stereotype.Service

interface UserService : IService<User> {

    /**
     * 根据age分页查询
     *
     * @param age 年龄
     * @return 返回用户列表
     *
     */
    fun selectAllAge(age: Int): List<User> = baseMapper.selectList(
        KtQueryWrapper(User::class.java).eq(User::age, age)
    )

    /**
     * 查询username中包含“n”的同时年龄在20~24区间的用户
     *
     * @return 返回用户列表
     */
    fun selectByNameBetweenAge(): List<User> = baseMapper.selectList(
        KtQueryWrapper(User::class.java).like(User::username, "n").between(User::age, 20, 24)
    )

    /**
     * 只查询username和age字段，select()通常和selectMaps()联合使用
     *
     * @return 返回用户username和age字段列表
     */
    fun selectMaps(): List<Map<String, Any>> = baseMapper.selectMaps(
        KtQueryWrapper(User::class.java).select(User::username, User::age)
    )

    /**
     * 子查询
     *
     * @return 返回用户username和age字段列表
     */
    fun selectInSql(): List<User> = baseMapper.selectList(
        KtQueryWrapper(User::class.java).inSql(User::id, "SELECT uid FROM t_user WHERE uid <=3")
    )

}

@Service
class UserServiceImp : UserService, ServiceImpl<UserMapper, User>()
